package zookeeper;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

public class ConfigurationServer {
    Configuration configuration = new Configuration();


    public void setDefault(){
        configuration.setDriverClassName("com.mysql.jdbc.Driver");
        configuration.setUrl("jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=UTF-8&RewriteBatchedStatements=true");
        configuration.setUsername("root");
        configuration.setPassword("root");
        configuration.setInitialSize("5");
        configuration.setMaxActive("10");
        configuration.setMaxWait("3000");
    }

    public static void main(String[] args) {
        ZkClient zk = new ZkClient("linux100:2181,linux101:2181,linux102:2181");
        zk.setZkSerializer(new ZkConfSerializer());
        ConfigurationServer configurationServer = new ConfigurationServer();
        configurationServer.setDefault();
        //在zookeeper上创建一个临时节点，其data是连接mysql的配置
        zk.createEphemeral("/mysql-configuration", configurationServer.configuration);

        //10秒之后修改连接的数据库
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        configurationServer.configuration.setUrl("jdbc:mysql://127.0.0.1:3306/db2?characterEncoding=UTF-8&RewriteBatchedStatements=true");
        zk.writeData("/mysql-configuration",configurationServer.configuration);

        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
